package com.whwj.heaven.auth.controller;


import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import com.whwj.heaven.auth.service.impl.HeavenUserDetailsService;
import com.wjwh.heaven.common.security.pojo.HeavenUserDto;
import com.wjwh.heaven.common.security.pojo.SecurityUser;
import com.wjwh.heaven.common.security.utils.SecurityUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.KeyPair;
import java.security.interfaces.RSAPublicKey;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取RSA公钥接口
 * @author: dm
 * @Date: 2020/10/29 11:31
 * @Description:
 */
@RestController
@AllArgsConstructor
public class KeyPairController {

    private final KeyPair keyPair;

    @GetMapping("/rsa/publicKey")
    public Map<String, Object> getKey() {
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAKey key = new RSAKey.Builder(publicKey).build();
        return new JWKSet(key).toJSONObject();
    }

    @GetMapping("/api/publicKey")
    public HashMap<Object, Object> publicKey() {
        SecurityUser user = SecurityUtils.getUser();
        HashMap<Object, Object> objectHashMap = new HashMap<>();
        objectHashMap.put("1",1);
        return objectHashMap;
    }


    @GetMapping("/rsa/publicKey2")
    public Object getKey2() {
        SecurityUser user = SecurityUtils.getUser();

        return user;
    }

    @GetMapping("/rsa/code")
    public Object getCode(String code) {
        System.out.println(code);
        return code;
    }

    private final HeavenUserDetailsService heavenUserDetailsService;

    @GetMapping("/hhhh")
    public Object hhhh(String code) {
        HeavenUserDto byUserDto = heavenUserDetailsService.findByUserDto(code);
        System.out.println(code);
        System.out.println(byUserDto.toString());
        return byUserDto;
    }
}